#include "EpilParameterListParser.h"
#include "EpilParserException.h"
#include "EpilPointerHelper.h"


EpilParameterListParser::EpilParameterListParser( EpilTokenStream & tokens )
: EpilSyntaxParserBase( tokens )
{
}


void EpilParameterListParser::Parse()
{
	// parlist -> [ param { `,' param } ]
	m_parameterList = new EpilParameterListCode( this->GetTokens().GetCurrentToken().location );

	if( this->GetTokens().GetCurrentToken().type == ETT_CloseParenthese )
	{
		m_parameterList->SetVariousParameters( false );
		return;
	}

	for(;;)
	{
		bool hasVariousParameters = false;
		switch( this->GetTokens().GetCurrentToken().type )
		{
		case ETT_Identifier:
			{
				m_parameterList->GetParameterNames().push_back( this->GetTokens().GetCurrentToken().text );
				this->GetTokens().MoveToNext();
			}
			break;
		case ETT_OpVariousParameters:
			{
				m_parameterList->SetVariousParameters( true );
				hasVariousParameters = true;
				this->GetTokens().MoveToNext();
			}
			break;
		default:
			EpilPointerHelper::SafeDelete( m_parameterList );
			throw EpilParserException( t("Identifier or \'...\' expected."), this->GetTokens().GetCurrentToken().location );
			break;
		}

		if( this->GetTokens().GetCurrentToken().type != ETT_OpComma )
		{
			if( hasVariousParameters == false )
			{
				m_parameterList->SetVariousParameters( false );
			}
			break;
		}
		this->GetTokens().MoveToNext();
	}
}